package com.igroupes.ovineherd.model.attribute;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.igroupes.ovineherd.dto.response.Meta;
import com.igroupes.ovineherd.entity.DataModelAttributeEntity;
import com.igroupes.ovineherd.enums.BooleanKey;
import com.igroupes.ovineherd.exception.ModelException;
import com.igroupes.ovineherd.model.IParentModelData;
import com.igroupes.ovineherd.model.Style.*;
import com.igroupes.ovineherd.model.dto.FieldApiResponse;
import com.igroupes.ovineherd.model.enums.FormKey;
import com.igroupes.ovineherd.model.enums.MySqlTypeKey;
import com.igroupes.ovineherd.model.utils.DbName;
import com.igroupes.ovineherd.query.FieldQuery;
import com.igroupes.ovineherd.query.ModelDataJsonAttributeQuery;
import com.igroupes.ovineherd.util.DateUtils;
import lombok.Data;
import lombok.ToString;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 基础字段
 */
@Data
@ToString(callSuper = true)
public class BasicAttribute {

    private static String NAMELABEL = "字段名称";
    private static Integer NAMEMAXLENGTH = 10;
    private static String NAMEDESC = "名称";
    private static String DESLABEL = "字段描述";
    private static Integer DESMAXLENGTH = 1000;
    private static String DESDESC = "字段底部显示的描述信息";
    private static String ISNULLLABEL = "是否必填";
    private static String ISNULLDESC = "限制填写数据是否必传";
    private static String ORDER_LABEl = "排序值";
    private static String ORDER_DESC = "从大到小";
    private static Integer ORDER_MIN = 1;
    private static Integer ORDER_MAX = 9999;

    protected TextStyle name;
    protected TextareaStyle desc; //字段描述
    protected SwitchStyle required; // 是否必填
    protected NumberStyle order; //排序

    protected BasicAttribute() {
        this.name = new TextStyle(NAMELABEL,"",true,NAMEMAXLENGTH,NAMEDESC);
        this.desc = new TextareaStyle(DESLABEL, "", false,  DESMAXLENGTH, DESDESC);
        this.required = new SwitchStyle(ISNULLLABEL, BooleanKey.TRUE.getCode(), false, ISNULLDESC);
        this.order = new NumberStyle(ORDER_LABEl,1,true, ORDER_DESC, ORDER_MIN,ORDER_MAX);
    }
    public JSONObject getAttribute(ModelDataJsonAttributeQuery modelDataJsonAttributeQuery) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("name", new TextStyle(NAMELABEL, modelDataJsonAttributeQuery.getName(),true,NAMEMAXLENGTH,NAMEDESC));
        jsonObject.set("desc", new TextareaStyle(DESLABEL, modelDataJsonAttributeQuery.getDesc(), false, DESMAXLENGTH, DESDESC));
        jsonObject.set("required",new SwitchStyle(ISNULLLABEL, modelDataJsonAttributeQuery.getIsNull(), false, ISNULLDESC));
        jsonObject.set("order", new NumberStyle(ORDER_LABEl,modelDataJsonAttributeQuery.getOrder(),true,ORDER_DESC,ORDER_MIN,ORDER_MAX));
        return jsonObject;
    }
    public Boolean verifyField(BasicAttribute basicAttribute) {
        new TextStyle().verify(basicAttribute.getName());
        if(ObjectUtil.isNotNull(basicAttribute.getRequired())){
            new SwitchStyle().verify(basicAttribute.getRequired());
        }
        new TextareaStyle().verify(basicAttribute.getDesc());
        new NumberStyle().verify(basicAttribute.getOrder());
        return true;
    }

    public Meta getMeta(ModelDataJsonAttributeQuery dataModelAttributeEntity){
        return new Meta(FormKey.TEXT.getCode(), true, false, true);
    }

    /**
     * 新版本的校验参数
     * @param basic
     */
    public Boolean verifyFieldV2(BasicAttribute basic) {
        new TextStyle().verify(new TextStyle(NAMELABEL, basic.getName().getValue(),true,NAMEMAXLENGTH,NAMEDESC));
        if(ObjectUtil.isNotNull(basic.getRequired())) {
            new SwitchStyle().verify(new SwitchStyle(ISNULLLABEL, basic.getRequired().getValue(), false, ISNULLDESC));
        }
        new TextareaStyle().verify(new TextareaStyle(DESLABEL, basic.getDesc().getValue(), false, DESMAXLENGTH, DESDESC));
        new NumberStyle().verify(new NumberStyle(ORDER_LABEl,basic.getOrder().getValue(),true,ORDER_DESC,ORDER_MIN,ORDER_MAX));
        return true;
    }
    public List<FieldQuery> sqlField(IParentModelData parentModelData, DataModelAttributeEntity dataModelAttributeEntityOld) {
        List<FieldQuery> fieldQueries = new ArrayList<>();
        BasicAttribute basicAttribute = parentModelData.getAttributes();
//        String id = IdUtil.simpleUUID();
//        parentModelData.setId(new HiddenStyle(id));
        String dateTime = DateUtils.getDateTime();
        if (dataModelAttributeEntityOld == null) {
            parentModelData.setAddTime(dateTime);
            parentModelData.setUpdateTime(dateTime);
        } else {
            parentModelData.setAddTime((String) ((Map) (dataModelAttributeEntityOld.getJson())).get("addTime"));
            parentModelData.setUpdateTime(dateTime);
        }
        FieldQuery fieldQuery = new FieldQuery(
                dataModelAttributeEntityOld == null ?
                        DbName.getDbName(Convert.toStr(basicAttribute.getName().getValue())) :
                        dataModelAttributeEntityOld.getDbName(),
                Convert.toStr(basicAttribute.getDesc().getValue()),
                ObjectUtil.isNull(basicAttribute.getRequired()) ? BooleanKey.TRUE.getCode() : Convert.toInt(basicAttribute.getRequired().getValue()));
        DataModelAttributeEntity dataModelAttributeEntity = new DataModelAttributeEntity(
                Convert.toStr(basicAttribute.getName().getValue())
                , Convert.toStr(basicAttribute.getDesc().getValue())
                , fieldQuery.getFieldName()
                , ObjectUtil.isNull(basicAttribute.getRequired()) ? BooleanKey.TRUE.getCode() : Convert.toInt(basicAttribute.getRequired().getValue())
                , new JSONObject(), parentModelData.getBeanType(), Convert.toInt(basicAttribute.getOrder().getValue()));
        fieldQuery.setDataModelAttributeEntity(dataModelAttributeEntity);
        fieldQuery.setType(MySqlTypeKey.VARCHAR.getCode());
        fieldQueries.add(fieldQuery);
        return fieldQueries;
    }
//    public List<FieldQuery> sqlFieldV2(IParentModelData parentModelData, DataModelAttributeEntity dataModelAttributeEntityOld) {
//        List<FieldQuery> fieldQueries = new ArrayList<>();
//        BasicAttribute basic = parentModelData.getAttributes();
//        FieldQuery fieldQuery = new FieldQuery(DbName.getDbName(Convert.toStr(basic.getName().getValue())),
//                Convert.toStr(basic.getDesc().getValue()),
//                Convert.toInt(basic.getIsNull().getValue()));
//        String dateTime = DateUtils.getDateTime();
//
//        if (dataModelAttributeEntityOld == null) {
//            parentModelData.setAddTime(dateTime);
//            parentModelData.setUpdateTime(dateTime);
//        } else {
//            parentModelData.setAddTime((String) ((Map) (dataModelAttributeEntityOld.getJson())).get("addTime"));
//            parentModelData.setUpdateTime(dateTime);
//        }
//        DataModelAttributeEntity dataModelAttributeEntity = new DataModelAttributeEntity(
//                Convert.toStr(basic.getName().getValue())
//                , Convert.toStr(basic.getDesc().getValue())
//                , fieldQuery.getFieldName()
//                , Convert.toInt(basic.getIsNull().getValue())
//                , new JSONObject(), parentModelData.getBeanType(), Convert.toInt(basic.getOrder().getValue()));
//        fieldQuery.setDataModelAttributeEntity(dataModelAttributeEntity);
//        fieldQuery.setType(MySqlTypeKey.VARCHAR.getCode());
//        fieldQueries.add(fieldQuery);
//        return fieldQueries;
//    }

    public List<FieldApiResponse> verifyRequestData(DataModelAttributeEntity modelAttributeEntity, Object value) {
        if(modelAttributeEntity.getIsNull().equals(BooleanKey.FALSE.getCode())){
            if(value == null){
                throw new ModelException(modelAttributeEntity.getName()+"值不能为空");
            }
        }
        return new ArrayList<>();
    }
    public JSONObject getAttributeStyle(ModelDataJsonAttributeQuery dataModelAttributeEntity) {
        return new JSONObject(new EmailStyle(
                dataModelAttributeEntity.getName(),"",
                dataModelAttributeEntity.getIsNull().equals(BooleanKey.TRUE.getCode())
        ), false);
    }

    /**
     * 转化查询值
     * @param value
     * @return
     */
    public Object getData(DataModelAttributeEntity modelAttributeEntity, Object value) {
        return value;
    }
}
